<?php
/***user auth***/

class SpAuth{
	private $db;							//数据库连接
	private $main_table='data_sp';			//用户表	
	public function __construct($db){
		$this->db=$db;
	}

	public function __destruct(){
		unset($this->db);
	}
	public function get_table_name(){
	    return $this->main_table;
	    
	}
	
	//登录验证
	private function check_pwd($username,$pwd){		
		$sql="SELECT sp_id,sp_name,sp_title,sp_check,sp_status FROM ".$this->main_table." WHERE sp_name='".inputSQL($username)."' AND sp_pwd='".base64_encode(md5($pwd))."'  ";
		$row=$this->db->getRow($sql);
		return $row;
	}

/*	public function go_login($row){
		$_SESSION['sp_auth'] = array(1,2);
		my_print($_SESSION['sp_auth']);
		return false;
	}*/
	
	//获取登录信息
	private function get_login_info($row){
		if(is_array($row)){
			//$this->db->debug(1,1);
			$this->db->update($this->main_table,array("sp_last_login"=>date("Y-m-d H:i:s"),"sp_last_login_ip"=>get_ip(),"sp_login_count"=>"sp_login_count+1"),"sp_id='".$row['sp_id']."'");
			$_SESSION['sp_auth'] = $row;
			//my_print($_SESSION['sp_auth']);
			return $row;
		}else{
			return false;
		}

	}

	//登录操作
	public function login($username,$pwd){
		$row = $this->check_pwd($username,$pwd);
		if($row['sp_status'] == '0'){
			my_jump('登陆失败,此账号禁止使用');
		}
		if($row['sp_check'] == '0'){
			my_jump('登陆失败,此账号正在审核中');
		}elseif($row['sp_check'] == '2'){
			my_jump('登陆失败,此账号审核没有通过');
		}
		return $this->get_login_info($row);

	}
	
	//判断是否登录中
	public function check_login(){
		$userinfo = $this->get_sp_info();
		if($userinfo['sp_id'] && $userinfo['sp_name'] && $userinfo['sp_status']=='1'){
			return true;	
		}else{
			return false;	
		}
	}

	//获取用户信息
	public function get_sp_info(){
		return $_SESSION['sp_auth'];
	}

	//退出登录
	public function logout(){
		$userinfo = $this->get_sp_info();
		unset($_SESSION['sp_auth']);
	}

	
/*	public function check_active(){
		$userinfo = $this->get_sp_info();
		if($userinfo['sp_check']=='0'){
			$more = '<a class="blue" href="'.SITE_ADDR.'user/user.htm">进入会员中心,进行激活</a>&nbsp;&nbsp;<a href="javascript:history.back()">返回</a>';
			my_jump('您的账号还没进行激活,请激活后再操作!  '.$more,'',$cf_self_path.'-act_index_rid_'.$rid.'.htm');
		}
	}*/


	/**
	* 	check user
	* 	@param array $data 
	* 	@param string $where 
	* 	@return int affected count
	*/
	public function check_sp($name){		
		$where = "sp_name='".inputSQL($name)."'";
		$sql="SELECT sp_id,sp_name,sp_title FROM ".$this->main_table." WHERE ".$where;
		$row=$this->db->getRow($sql);
		return $row;
	}
	

	
	/**
	* 	update user
	* 	@param array $data 
	* 	@param string $where 
	* 	@return int affected count
	*/
	public function update_data($data,$where){
		if(!is_array($data))
		my_jump('输入的参数有误');
		$result = $this->db->update($this->main_table,$data,$where);
		return $result;
	}

}



?>